<template>
  <div class="search-div">
  


<el-dialog v-model="dialogVisible" title="宣教" width="30%">
   <el-form label-width="120px">
       

         <!-- <el-form-item label="宣教内容">
         <el-input
           type="textarea"
           :rows="2"
           placeholder="请输入内容"
           v-model="healtheducation.content">
         </el-input>
         </el-form-item> -->

         <el-form-item label="文章">
         <el-input v-model="article.articleName"/>
         </el-form-item> 
  
       <el-form-item>
           <el-button type="primary" @click="submit()">提交</el-button>
           <el-button @click="dialogVisible = false">取消</el-button>
       </el-form-item>
   </el-form>
</el-dialog>

       <!-- 添加按钮 -->
       <div class="tools-div">
           <el-button type="success" size="small" @click="addShow">添加</el-button>
       </div>
       
       <!--- 角色表格数据 -->
       <el-table :data="list" style="width: 100%">
           <el-table-column prop="id" label="序号" width="170" />
           <el-table-column prop="articleName" label="文章名称" width="250" />
           <!-- <el-table-column prop="content" label="内容" width="250" /> -->
           <el-table-column label="操作" align="center" width="180" #default="scope">
               <el-button type="danger" size="small" @click="deleteById(scope.row)">
                   删除
               </el-button>
               <el-button type="primary" size="small" @click="editShow(scope.row)">
           修改
         </el-button>
           </el-table-column>
           
     

       </el-table>

       <!--分页条-->
       <el-pagination
              v-model:current-page="pageParams.page"
              v-model:page-size="pageParams.limit"
              :page-sizes="[10, 20, 50, 100]"
              @size-change="fetchData"
              @current-change="fetchData"
              layout="total, sizes, prev, pager, next"
              :total="total"
/>   
 </div>

</template>

<script setup>
import { ref , onMounted } from 'vue';
import { articlePageList ,Savearticle ,Updatearticle,Delarticle} from '@/api/yingfei/article'
import { ElMessage, ElMessageBox } from 'element-plus'


// 分页条总记录数
let total = ref(0)

// 定义表格数据模型
let list = ref([])

//分页数据
const pageParamsForm = {
 page: 1, // 页码
 limit: 3, // 每页记录数
}
const pageParams = ref(pageParamsForm)     // 将pageParamsForm包装成支持响应式的对象


// 搜索表单数据
const queryDto = ref({
sname: "",
articleId: "",
articleName: "",
dispatchDate: ""

})

// 页面加载完毕以后请求后端接口获取数据
onMounted(() => {
   fetchData() ;

})

// 搜索按钮点击事件处理函数




// 远程调用后端分页查询接口
const fetchData = async () => {
   const {data , code , message } = await articlePageList(pageParams.value.page , pageParams.value.limit ) ;
   list.value = data.list ;
   total.value = data.total
}

// 删除数据
const deleteById = (row) => {
   ElMessageBox.confirm('此操作将永久删除该记录, 是否继续?', 'Warning', {
       confirmButtonText: '确定',
       cancelButtonText: '取消',
       type: 'warning',
   }).then(async () => {
      const {code } = await Delarticle(row.id)
      if(code === 200) {
           ElMessage.success('删除成功')
           pageParams.value.page = 1
           fetchData()
      }
   })
}

const defaultForm = {       //页面表单数据
 id: '',
 articleId : '',
 categoryId: ''
}

const article = ref(defaultForm)    
const dialogVisible = ref(false)

//进入添加
const addShow = () => {
  article.value = {}
   dialogVisible.value = true
}

// 进入修改
const editShow = row => {
  article.value = row
 dialogVisible.value = true
}

//添加
const submit = async()=>{
   if(!article.value.id) {
       const { code } = await Savearticle(article.value) ;
       if(code === 200) {
           dialogVisible.value = false
           ElMessage.success('操作成功')
           fetchData()
       }
//修改
   }else {
       const { code } = await Updatearticle(article.value) ;
       if(code === 200) {
           dialogVisible.value = false
           ElMessage.success('操作成功')
           fetchData()
       }
   }
}




</script>

<style scoped>

.search-div {
 margin-bottom: 10px;
 padding: 10px;
 border: 1px solid #ebeef5;
 border-radius: 3px;
 background-color: #fff;
}

.tools-div {
 margin: 10px 0;
 padding: 10px;
 border: 1px solid #ebeef5;
 border-radius: 3px;
 background-color: #fff;
}

</style>